package com.example.rediscacheproject.controller;

import com.example.rediscacheproject.entity.User;
import com.example.rediscacheproject.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("根据ID获取用户")
    @GetMapping("/{id}")
    public Map<String, Object> getUserById(
            @ApiParam("用户ID") @PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            User user = userService.getUserById(id);
            if (user != null) {
                result.put("code", 200);
                result.put("message", "获取成功");
                result.put("data", user);
            } else {
                result.put("code", 404);
                result.put("message", "用户不存在");
                result.put("data", null);
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取失败：" + e.getMessage());
            result.put("data", null);
        }
        return result;
    }

    @ApiOperation("获取所有用户")
    @GetMapping
    public Map<String, Object> getAllUsers() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<User> users = userService.getAllUsers();
            result.put("code", 200);
            result.put("message", "获取成功");
            result.put("data", users);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "获取失败：" + e.getMessage());
            result.put("data", null);
        }
        return result;
    }

    @ApiOperation("创建用户")
    @PostMapping
    public Map<String, Object> createUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = userService.createUser(user);
            if (success) {
                result.put("code", 200);
                result.put("message", "创建成功");
                result.put("data", user);
            } else {
                result.put("code", 500);
                result.put("message", "创建失败");
                result.put("data", null);
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "创建失败：" + e.getMessage());
            result.put("data", null);
        }
        return result;
    }

    @ApiOperation("更新用户")
    @PutMapping
    public Map<String, Object> updateUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = userService.updateUser(user);
            if (success) {
                result.put("code", 200);
                result.put("message", "更新成功");
                result.put("data", user);
            } else {
                result.put("code", 500);
                result.put("message", "更新失败");
                result.put("data", null);
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "更新失败：" + e.getMessage());
            result.put("data", null);
        }
        return result;
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteUser(
            @ApiParam("用户ID") @PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = userService.deleteUser(id);
            if (success) {
                result.put("code", 200);
                result.put("message", "删除成功");
                result.put("data", null);
            } else {
                result.put("code", 500);
                result.put("message", "删除失败");
                result.put("data", null);
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "删除失败：" + e.getMessage());
            result.put("data", null);
        }
        return result;
    }
}
